﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    /// <summary>
    /// CNO ELB数据访问类
    /// </summary>
    public class CompositeNodeObjectElementELBDao : DaoService<CompositeNodeObjectElement>
    {
        public override string TableName => "FAS_CompositeNodeObjectElement";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "CompositeNodeObjectID";
        public string MetaDataForeignColumn => "CompositeDataObjectID";
        public override List<string> ExceptColumnName { get; } =
            DaoUtil.GetExceptionColumn<CompositeNodeObjectElement>(
                new List<string>
                {
                    //"DisplayName",
                    //"Alias",
                    "IsForQuery",
                });

        public CompositeNodeObjectElementELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<CompositeNodeObjectElement> GetCompositeNodeObjectElements(string cnoID)
        {
            var result = new List<CompositeNodeObjectElement>();
            if (string.IsNullOrEmpty(cnoID))
            {
                return result;
            } 
            var dt = base.GetByFilter($"{ForeignColumn}='{cnoID}'");
            result = DaoUtil.ConvertToEntity<CompositeNodeObjectElement>(dt, this.ExceptColumnName) ?? result;
            return result?.OrderBy(ele => ele.Sort).ThenBy(ele => ele.Name)?.ToList();
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<CompositeNodeObjectElement> GetCompositeNodeObjectElementsByMetaDataForeignKey(string metaDataID)
        {
            var result = new List<CompositeNodeObjectElement>();
            if (string.IsNullOrEmpty(metaDataID))
            {
                return result;
            }
            var dt = base.GetByFilter($"{MetaDataForeignColumn}='{metaDataID}'");
            result = DaoUtil.ConvertToEntity<CompositeNodeObjectElement>(dt, this.ExceptColumnName) ?? result;
            return result?.OrderBy(ele => ele.Sort).ThenBy(ele => ele.Name)?.ToList();
        }

        /// <summary>
        /// 添加CNOElement
        /// </summary>
        /// <param name="compositeNodeObjectElements"></param>
        public void CreateCompositeNodeObjectElement(List<CompositeNodeObjectElement> compositeNodeObjectElements)
        {
            try
            {
                //Precision是MySQL中关键词，所以采用自己生成的SQL
                DaoUtil.ExecuteInsertSQL(compositeNodeObjectElements, this);    //创建CNO.Element
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.CreateCNOEleError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除CNOElement
        /// </summary>
        /// <param name="compositeNodeObjectElements"></param>
        public void DeleteCompositeNodeObjectElement(string cnoID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, cnoID));
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.DeleteCNOEleError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除CNOElement
        /// </summary>
        /// <param name="compositeNodeObjectElements"></param>
        public void DeleteByMetaDataForeignKey(string metaDataID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.MetaDataForeignColumn, metaDataID));
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.DeleteCNOEleError, e.Message, e);
            }
        }

    }
}
